﻿Imports System.ComponentModel
Imports ICSharpCode.SharpZipLib.Zip
Imports System.IO
Imports ICSharpCode.SharpZipLib.Core

Public Class ZipCreateForm

  Private _bgworker As New BackgroundWorker
  Private _config As Configuration = Configuration.GetObject
  Private _wallpaper As Wallpaper 

  Public Sub New(ByVal w As Wallpaper)
    InitializeComponent()
    _wallpaper = w
  End Sub

  Private Sub ZipCreateForm_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    While _bgworker.IsBusy = True
      'warten bis der thread fertig ist
    End While
  End Sub

  Private Sub ZipCreateForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Me.Label1.Text = ""
    Me.Text = ""
    BeendenButton.Enabled = False 
    AddHandler _bgworker.DoWork, AddressOf Me.BgWorkerCreate
    AddHandler _bgworker.ProgressChanged, AddressOf Me.BgWorkerProgressChanged
    AddHandler _bgworker.RunWorkerCompleted, AddressOf Me.BgWorkerCompleted

    _bgworker.WorkerReportsProgress = True
    
    StartCreateZIP(_wallpaper)

  End Sub

  Public Sub StartCreateZIP(ByVal wl As Wallpaper)
    Dim sfd As New SaveFileDialog()
    sfd.FileName = wl.FullName & ".zip"
    Dim args As New ArrayList
    Me.Text = "Erstelle ZIP für " & wl.FullName
    If sfd.ShowDialog() = Windows.Forms.DialogResult.Cancel Then
      Close()
    Else
      args.Add(wl)
      args.Add(sfd.FileName)
      _bgworker.RunWorkerAsync(args)
    End If
  End Sub

  Private Sub BgWorkerCreate(ByVal sender As Object, ByVal e As DoWorkEventArgs)
    Dim wl As Wallpaper = DirectCast(e.Argument, ArrayList)(0)
    Dim wr() As Wallpaper = ServiceManager.GetServiceManager.GetWallpaperService.GetWallpaperByName(wl.Name).ToArray

    Dim buffer(4096) As Byte
    Dim zipname As String = DirectCast(e.Argument, ArrayList)(1)

    _bgworker.ReportProgress(wr.Length, Nothing)

    Dim s As New ZipOutputStream(IO.File.Create(zipname))

    s.SetLevel(9)

    For i As Integer = 0 To wr.Length - 1 Step 1
      Dim entry As New ZipEntry(_config.SkinsPath & wr(i).Filename)
      s.PutNextEntry(entry)
      Dim fs As FileStream = IO.File.OpenRead(_config.SkinsPath & wr(i).Filename)
      StreamUtils.Copy(fs, s, buffer)
      _bgworker.ReportProgress(1, wr(i))
    Next
    s.CloseEntry()
    s.Close()
  End Sub

  Private Sub BgWorkerProgressChanged(ByVal sender As Object, _
  ByVal e As System.ComponentModel.ProgressChangedEventArgs)
    If e.UserState Is Nothing Then
      Me.ZipProgressBar.Maximum = e.ProgressPercentage
      Me.ZipProgressBar.Value = 0
    Else
      Me.ZipProgressBar.Value += 1
      Me.Label1.Text = "füge hinzu: " & DirectCast(e.UserState, Wallpaper).Filename
    End If
  End Sub

  Private Sub BgWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs)
    Me.Label1.Text = "Abgeschlossen"
    Me.ZipProgressBar.Value = ZipProgressBar.Maximum
    BeendenButton.Enabled = True 
    If CloseCheckBox.Checked = True Then
      Close()
    End If
  End Sub

  Private Sub BeendenButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BeendenButton.Click
    Close()
  End Sub
End Class